﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblContactPersonDAL:Iapp_tblContactPerson
    {
        HKEA_CMSEntities model;
        app_tblContactPerson cp;
        private app_tblContactPersonDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblContactPersonDAL GetInstance()
        {
            return new app_tblContactPersonDAL();
        }

        public int InsertENT(app_tblContactPerson ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblContactPerson.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblContactPerson.Max(a => a.AppContactPersonUID);
            return 0;
        }

        public List<app_tblContactPerson> GetListByENT(app_tblContactPerson ent)
        {
            var result = model.app_tblContactPerson.Where(a => a.AppContactPersonUID > 0);
            if (ent.AppContactPersonUID > 0)
                result = result.Where(a=>a.AppContactPersonUID==ent.AppContactPersonUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            return result.ToList();
        }

        public int GetCountByENT(app_tblContactPerson ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblContactPerson ent)
        {
            var ret = model.app_tblContactPerson.Where(a=>a.AppContactPersonUID==ent.AppContactPersonUID).FirstOrDefault();
            if (ret != null)
            {
                ret.ActivityUID = ent.ActivityUID;
                ret.AddressFollowCust = ent.AddressFollowCust;
                ret.AddressUID = ent.AddressUID;
                ret.AddressUID_CP = ent.AddressUID_CP;
                ret.AddressUID_Cust = ent.AddressUID_Cust;
                ret.AppAddressUID = ent.AppAddressUID;
                ret.AppContactInfoUID = ent.AppContactInfoUID;
                ret.AppContactPersonUID = ent.AppContactPersonUID;
                ret.AppRelatedContactPersonUID = ent.AppRelatedContactPersonUID;
                ret.ChristianName = ent.ChristianName;
                ret.ContactInfoFollowCust = ent.ContactInfoFollowCust;
                ret.ContactInfoUID = ent.ContactInfoUID;
                ret.ContactInfoUID_CP = ent.ContactInfoUID_CP;
                ret.ContactInfoUID_Cust = ent.ContactInfoUID_Cust;
                ret.ContactPersonUID = ent.ContactPersonUID;
                ret.Deleted = ent.Deleted;
                ret.FirstName_Chi = ent.FirstName_Chi;
                ret.FirstName_Eng = ent.FirstName_Eng;
                ret.HKID_PassportID = ent.HKID_PassportID;
                ret.LastModifyDate = DateTime.Now;
                ret.LastName_Chi = ent.LastName_Chi;
                ret.LastName_Eng = ent.LastName_Eng;
                ret.OtherTitle_Chi = ent.OtherTitle_Chi;
                ret.OtherTitle_Eng = ent.OtherTitle_Eng;
                ret.Position = ent.Position;
                ret.RelatedContactPersonUID = ent.RelatedContactPersonUID;
                ret.Relationship = ent.Relationship;
                ret.Remark = ent.Remark;
                ret.SalutationUID = ent.SalutationUID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(app_tblContactPerson ent)
        {
            var ret = model.app_tblContactPerson.Where(a => a.AppContactPersonUID == ent.AppContactPersonUID).FirstOrDefault();
            if (ret != null)
            {
                model.app_tblContactPerson.DeleteObject(ret);
            }
            return model.SaveChanges();
        }


        public int DeleteByKeyList(List<int> list)
        {
            foreach (var temp in list)
            {
                var result = model.app_tblContactPerson.Where(a => a.AppContactPersonUID == temp).FirstOrDefault();
                if (result != null)
                    model.app_tblContactPerson.DeleteObject(result);
            }
            return model.SaveChanges();
        }
    }
}
